import * as api from '../services/example'
 export default {
  namespace:'product',
  state:{
    productList:[
      {
        name: '华为'
      },
      {
        name: '小米'
      },
      {
        name: 'vivo'
      },
      {
        name: 'oppo'
      }
    ]
  },
  reducers:{
    updateList(state,action){
      let productList=[
        {
          name: '华为'
        },
        {
          name: '小米'
        },
        {
          name: 'vivo'
        },
        {
          name: 'oppo'
        }
      ]
      if(action.payload){
        productList=productList.concat(action.payload)
      }
      return  {
        productList
      }
    },
    reduceList(state,action){
      let result={...state}
      result.productList.splice(action.index,1)
      return result
    }
  },
  effects:{
    *reduceListSync({index},{call , put}){
      yield put({
        type:'reduceList',
        index
      })
    },
    *updateListHttp({payload},{call,put}){
      //网络请求
      const result=yield call(api.getProduct,payload)
      const data=result.data
      if(data){
        yield put({
          type:'updateList',
          payload:[data]
        })
      }
    },
  },
  subscriptions:{
    hello({dispatch, history}) {
      console.log(dispatch,history)
    }
  }
}
